package encome.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import encome.work.dao.Menu;
import encome.work.mapper.MenuMapper;
import encome.work.service.MenuService;
import encome.work.utils.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ajie
 * @createTime 2021年08月11日 20:42:00
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RedisUtils redisUtil;

    @Override
    public Result findParent() {
        return Result.success("查询父级菜单成功", menuMapper.findParent());
    }

    @Override
    public Result findPage(QueryInfo queryInfo) {
        Page<Menu> menuPage = new Page<>(queryInfo.getPageNumber(), queryInfo.getPageSize());
        menuMapper.findPage(menuPage, queryInfo.getQueryString());
        return Result.success("分页查询成功", menuPage);
    }

    @Override
    public Result insert(Menu menu) {
        menuMapper.insert(menu);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("添加菜单数据成功");
    }

    @Override
    public Result delete(Long id) {
        menuMapper.deleteById(id);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("删除菜单数据成功");
    }

    @Override
    public Result update(Menu menu) {
        menuMapper.updateById(menu);
        redisUtil.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("修改菜单数据成功");
    }
}
